import { outputResult } from '../../src/testCase';
import * as process from 'process';
import * as outputs from '../../src/outputs'
import sinon from 'sinon'
import {expect} from 'chai'

describe('outputResult', () => {
    let sandbox: sinon.SinonSandbox;
    let addOutputStub : sinon.SinonStub;
    let uploadReportStub : sinon.SinonStub;

    beforeEach(() => {
        sandbox = sinon.createSandbox();
        addOutputStub = sandbox.stub(outputs, 'addOutput');
        //eslint-disable-next-line @typescript-eslint/no-var-requires
        uploadReportStub = sandbox.stub(require('../../src/testCase/uploadReport'), 'uploadReport');
        process.env['stepIdentifier'] = 'stepId'
        process.env['name'] = 'test-step'


    });
    afterEach(() => {
        sandbox.restore()
    });

  it('should skip coverage if it is undefined', async () => {
      uploadReportStub.resolves('objectKey')
      const testResult = {
          reportLocalPath: 'path/to/report.html'
      };

      await outputResult(testResult);
      expect(uploadReportStub.calledOnce).to.be.true;
      expect(addOutputStub.calledWith(`[stepId]STAT_INFO_TITLE:test-step`)).to.be.true;
      expect(addOutputStub.calledWith(`[stepId]STAT_TITLE__REPORT: report`)).to.be.true;
      expect(addOutputStub.callCount).to.equal(4)
  });


    it('should output coverage and report', async () => {
        uploadReportStub.resolves('objectKey')
        const testResult = {
            reportLocalPath: 'path/to/report.html',
            coverage: {
                failed: 1,
                passed: 2,
                passedRate: 83.33,
                skipped: 3,
                total: 6
            }
        };

        await outputResult(testResult);
        expect(uploadReportStub.calledOnce).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_INFO_TITLE:test-step`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_TITLE__REPORT: report`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_TITLE_PassedRate: PassedRate`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_NAME_PassedRate: test case`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_VALUE_PassedRate: 83.33`)).to.be.true;
        expect(addOutputStub.callCount).to.equal(24)
    });

    it('should output coverageRate and report', async () => {
        uploadReportStub.resolves('objectKey')
        const testResult = {
            reportLocalPath: 'path/to/report.html',
            coverageRate: {
                lineCoverageRate: 59.57,
                branchCoverageRate: 42.31,
                methodCoverageRate: 54.89,
                classCoverageRate: 64,
                instructionCoverageRate: 60.52
            }
        };
        await outputResult(testResult);
        expect(uploadReportStub.calledOnce).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_INFO_TITLE:test-step`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_TITLE__REPORT: report`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_TITLE_LineCoverageRate: LineCoverageRate`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_NAME_LineCoverageRate: coverage`)).to.be.true;
        expect(addOutputStub.calledWith(`[stepId]STAT_VALUE_LineCoverageRate: 59.57`)).to.be.true;
    });

});
